package game.views;

import hx.geom.Rectangle;
import game.shader.HeadShader;
import game.data.XlsData;
import game.views.CDDisplay.CDDisplayData;
import haxe.Timer;
import hx.events.Keyboard;
import hx.display.ArrayCollection;
import motion.Actuate;
import game.event.RoleEvent;
import game.worlds.GameRole;
import hx.display.Box;

/**
 * 生命值渲染UI组件
 */
@:build(hx.macro.UIBuilder.build("assets/views/HeadHpLine.xml"))
class HeadHpLine extends Box {
	/**
	 * CD显示列表
	 */
	public var cdListView:CDListView;

	override function onInit() {
		super.onInit();
		this.cdListView = new CDListView();
		this.addChild(cdListView);
		cdListView.data = new ArrayCollection([]);
		cdListView.scaleX = cdListView.scaleY = 0.3;
		cdListView.x = 30;
		cdListView.y = 180;
		this.winTimes = 0;
	}

	/**
	 * 胜利次数
	 */
	public var winTimes(get, set):Int;

	private var __winTimes:Int = 0;

	private function get_winTimes():Int {
		return __winTimes;
	}

	private function set_winTimes(value:Int):Int {
		__winTimes = value;
		for (i => object in winbox.children) {
			if (__winTimes > i) {
				object.visible = true;
			} else {
				object.visible = false;
			}
		}
		return value;
	}

	override function set_scaleX(value:Float):Float {
		// 人物名称也要翻转
		this.nameLabel.scaleX = value > 0 ? 1 : -1;
		this.cdListView.scaleX = 0.3 * (value > 0 ? 1 : -1);
		if (value < 0) {
			this.cdListView.x = 30 - this.cdListView.width;
		} else {
			this.cdListView.x = 30;
		}
		this.cdListView.itemRendererRecycler.args = [value];
		return super.set_scaleX(value);
	}

	public var role:GameRole;

	/**
	 * 绑定角色
	 * @param role 
	 */
	public function bindRole(role:GameRole):Void {
		if (this.role != null) {
			this.cdMaps = [];
			this.cdListView.data.clear();
			this.role.removeEventListeners();
			this.role.cdData.clearAll();
		}
		this.role = role;
		var data = XlsData.roles.getDataByRoleid(role.assets.id);
		if (data != null) {
			this.roleNameLabel.data = data.name;
			this.head.shader = new HeadShader(role.world.assets.getBitmapData(data.roleid));
		}
		this.setProgress(role.data.hp / role.data.maxHp);
		role.addEventListener(RoleEvent.HURT, onStateChange);
		role.addEventListener(RoleEvent.ADD_HP, onStateChange);
		role.addEventListener(RoleEvent.SKILL_END, onSkillEnd);
		this.nndline.maskRect = new Rectangle(0, 0, nndline.data.width, nndline.data.height);
	}

	public var cdMaps:Map<String, CDDisplayData> = [];

	private function onSkillEnd(e:RoleEvent):Void {
		// 技能开始时，设置CD显示
		for (key => value in role.cdData.cdNames) {
			if (!cdMaps.exists(key)) {
				var action = role.assets.data.actions.get(key);
				cdMaps.set(key, {
					maxCd: action.cd,
					groupKeys: action.getCDGroupKeys(),
					data: value,
				});
				if (action.cd > 0)
					this.cdListView.data.add(cdMaps.get(key));
			}
		}
	}

	override function onUpdate(dt:Float) {
		super.onUpdate(dt);
		for (key => value in cdMaps) {
			if (!role.cdData.isCDing(key)) {
				cdMaps.remove(key);
				this.cdListView.data.remove(value);
			}
		}
		this.cdListView.updateAllData();
		// MP值同步
		for (i in 0...10) {
			var mpId = "mp" + (i + 1);
			var mp:MpPoint = cast this.getChildById(mpId);
			mp.visible = (i + 1) * 100 <= role.data.maxMp;
			var cMp = role.data.mp - i * 100;
			cMp = Std.int(Math.max(0, Math.min(100, cMp)));
			mp.setProgress(cMp / 100);
		}
		if (__progressChanged && !role.isHurt) {
			__progressChanged = false;
			Actuate.tween(this.redbg, 0.5, {width: 400 * __progress}).delay(0.5);
		}
		// 气力值
		nndline.maskRect.width = role.data.effort / role.data.maxEffort * nndline.data.width;
	}

	private function onStateChange(e:RoleEvent):Void {
		this.setProgress(role.data.hp / role.data.maxHp);
	}

	private var __progress:Float = 0;
	private var __progressChanged:Bool = false;

	private var __hprect:Rectangle = new Rectangle();

	/**
	 * 设置进度值
	 * @param value 进度值0-1
	 */
	public function setProgress(value:Float):Void {
		__hprect.width = value * line.data.width;
		__hprect.height = line.data.height;
		this.line.maskRect = __hprect;
		this.__progress = value;
		this.__progressChanged = true;
	}
}
